<?php namespace app\common\service;
use  \app\common\model\AuctionOrder as AuctionOrderModel;
use app\common\model\User;


/**
 * Class AuctionOrder
 * @package app\common\service
 */
class AuctionOrder extends Base{
    /**
     * AuctionOrder constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $AuctionOrderModel= new AuctionOrderModel();
        $this->model = $AuctionOrderModel;
    }


    /**
     * @param $where
     * @param $page
     * @param int $limit
     * @return array
     */
    public function getList($where, $page, $limit=10){
        try{
            $data=$this->paginatet($where,'*','createtime desc',$limit,$page,'');

        }catch (\Exception $e){
            $data=[];
        }
        return $data;
    }

    /**
     * 获取money总价格
     * @param $where
     * @return float|int
     */
    public function getSum($where){
        try{
            $price=$this->model->where($where)->sum('money');
        }catch (\Exception $e){
            $price=0;
        }
        return $price;
    }


    /**
     * @param $where
     * @param $page
     * @param int $limit
     * @return array
     */
    public function getListNew($where, $page, $limit=10){
        try{
            $data=$this->paginatet($where,'*',['createtime'=>'desc'],$limit,$page,'');
            if(!empty($data)){
                foreach ($data as $key=>$val){
                    $user=User::get($val['user_id']);
                    $val->name=$user->nickname;

                }
            }
        }catch (\Exception $e){
            $data=[];
        }
        return $data;
    }

    /**
     * 获取当前用竞拍详情之前竞拍的总价格计算差价
     * @param $where
     * @return float|int
     */
    public function getPrice($where){
        try{
            $number=$this->model->where($where)->sum('money');
        }catch (\Exception $e){
            $number=0;
        }
        return $number;
    }


    /**
     * @param $data
     * @return array|bool
     */
    public function add($data)
    {
       try{
           $data= parent::add($data);
       }catch (\Exception $e){
           $data=[];
       }
       return $data;
    }


    /**
     * @param $map
     * @return array
     */
    public function find($map)
    {
        try{
            $data=parent::find($map);

        }catch (\Exception $e){
            $data=[];
        }
        return $data;
    }


    public function findOne($map)
    {
        try{
            $data=$this->model->where($map)->order('createtime','desc')->find();

        }catch (\Exception $e){
            $data=[];
        }
        return $data;
    }

    /**
     * @param array $map
     * @return int|string
     */
    public function count($map = [])
    {
        try{
            $number=$this->model->count();
        }catch (\Exception $e){
            $number=0;
        }
        return $number;
    }

    /**
     * 获取当条字段返回数组
     * @param $map
     * @param $filed
     * @return array
     */
    public function filed($map,$filed){
        try{
            $data=$this->model->where($map)->column($filed);
        }catch (\Exception $e){
            $data=[];
        }
        return $data;
    }


}